// Twitter YQL implementation

URLS = {
    BASE_OAUTH: "https://api.twitter.com/oauth/",
	BASE_YQL: "https://query.yahooapis.com/v1/public/yql"
};

QUERY = {
	REQUEST_TOKEN:		"select * from twitter.oauth.requesttoken",
	ACCESS_TOKEN:		"select * from twitter.oauth.accesstoken where oauth_verifier='@oauth_verifier' and oauth_token='@oauth_request_token' and oauth_token_secret='@oauth_request_token_secret'",
	VERIFY_CREDENTIALS:	"select * from twitter.oauth.verifycredentials where oauth_token='@oauth_access_token' and oauth_token_secret='@oauth_access_token_secret'",
	TREND_LOCATION:		"select * from twitter.oauth.trends.location where woeid='@woeid' and oauth_token='@oauth_access_token' and oauth_token_secret='@oauth_access_token_secret'",
	TREND_DAILY:		"select * from twitter.oauth.trends.daily where oauth_token='@oauth_access_token' and oauth_token_secret='@oauth_access_token_secret'",
	TREND_WEEKLY:		"select * from twitter.oauth.trends.weekly where oauth_token='@oauth_access_token' and oauth_token_secret='@oauth_access_token_secret'",
	TREND_AVAILABLE:	"select * from twitter.oauth.trends.available where oauth_token='@oauth_access_token' and oauth_token_secret='@oauth_access_token_secret'",
	SEARCH:				"select * from twitter.oauth.search where q='@query' and oauth_token='@oauth_access_token' and oauth_token_secret='@oauth_access_token_secret'"
};

var user = new TwitterUser();

function YQLRequest(query, parser) {
	// Properties
	var _query = query;
	var _properties = new Object();
	this.parser = parser;
	this.callback = null;
	this.onReady = null;

	// Methods
	this.SetProperty = function(name, value) {
		_properties[name] = value;
	};
	
	this.GetQueryAsURL = function() {
		var query = _query;
		query = query.replace('@oauth_request_token', user.GetRequestToken()); 
		query = query.replace('@oauth_request_token_secret', user.GetRequestTokenSecret());
		query = query.replace('@oauth_access_token', user.GetAccessToken()); 
		query = query.replace('@oauth_access_token_secret', user.GetAccessTokenSecret());
		
		for (var i in _properties) {
			query = query.replace('@' + i, _properties[i]);
		}
		
		return URLS.BASE_YQL + '?q=' + encodeURIComponent(query) + "&format=json&env=store://sites.google.com/twittrendoauth";
	};
};

function YQLHandler() {
	// Properties
	
	// Methods
	function CheckResponse(response) {
		if (response.readyState == 4) {
			if (response.status == 200) {
				return true;
			} else if (request.status == 400) {
				return false;
			}
		}
	};
	
	function DataCB() {
		if (CheckResponse(this)) {
			var resp = JSON.parse(this.responseText);
			if (resp == null) {
				return;
			}
				
			this.yqlRequest.parser.Parse(resp);
			this.yqlRequest.onReady();
		}
	};
	
	function RequestTokenCB() {
		if (CheckResponse(this)) {
			var resp = JSON.parse(this.responseText);
			if (resp == null) {
				return;
			}
				
			var paramMap = OAuth.getParameterMap(resp.query.results.result);
			user.SetRequestToken(paramMap['oauth_token'], paramMap['oauth_token_secret']);
			
			chrome.tabs.create({"url": URLS.BASE_OAUTH + 'authorize?oauth_token=' + paramMap['oauth_token'], "selected": true});
		}
	};

	function AccessTokenCB() {
		if (CheckResponse(this)) {
			var resp = JSON.parse(this.responseText);
			if (resp == null) {
				return;
			}
				
			var paramMap = OAuth.getParameterMap(resp.query.results.result);
			user.SetAccessToken(paramMap['oauth_token'], paramMap['oauth_token_secret'], paramMap['user_id'], paramMap['screen_name']);
			this.yqlRequest.onReady();
		}
	};
	
	this.GetRequestToken = function() {
		yqlRequest = new YQLRequest(QUERY.REQUEST_TOKEN);
		yqlRequest.callback = RequestTokenCB;
		this.MakeRequest(yqlRequest);
	};

	this.GetAccessToken = function(verifier, callback) {
		yqlRequest = new YQLRequest(QUERY.ACCESS_TOKEN);
		yqlRequest.SetProperty('oauth_verifier', verifier);
		yqlRequest.callback = AccessTokenCB;
		yqlRequest.onReady = callback;
		this.MakeRequest(yqlRequest);
	};

	this.MakeRequest = function(yqlRequest) {
		if (!yqlRequest) {
			return;
		}
		if (!yqlRequest.callback) {
			yqlRequest.callback = DataCB;
		}
		var request = createCORSRequest("GET", yqlRequest.GetQueryAsURL());
		if (request) {
			request.onreadystatechange = yqlRequest.callback;
			request.yqlRequest = yqlRequest;
			request.send();
		}
	};
};